﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EyeGuard
{
	public partial class NotifyForm : Form
	{
		Timer relaxTimer = new Timer();

		public NotifyForm(Screen screen)
		{
			InitializeComponent();

			InitForm(screen);
			InitTipsLabel(screen);
			InitRelaxProgressBar(screen);
		}

		private void InitForm(Screen screen)
		{
			this.StartPosition = FormStartPosition.Manual;
			this.Location = new Point(screen.Bounds.Left, screen.Bounds.Top);
			this.FormBorderStyle = FormBorderStyle.None;
			this.WindowState = FormWindowState.Maximized;
		}

		private void InitTipsLabel(Screen screen)
		{
			var x = 0 + screen.Bounds.Width / 2 - TipsLabel.Width / 2;
			var y = screen.Bounds.Height * 1 / 3 - TipsLabel.Height / 2;
			TipsLabel.Location = new Point(x, y); // offset to parent window
		}

		private void InitRelaxProgressBar(Screen screen)
		{
			var relaxTime = Properties.Settings.Default.relaxTime * 60; // minutes to seconds

			RelaxProgressBar.Minimum = 0;
			RelaxProgressBar.Maximum = relaxTime;
			RelaxProgressBar.Value = relaxTime;
			RelaxProgressBar.Location = new Point(0, screen.Bounds.Height * 618 / 1000); // offset to parent window
			RelaxProgressBar.Height = 1;
			RelaxProgressBar.Width = screen.Bounds.Width;
		}

		private void NotifyForm_Load(object sender, EventArgs e)
		{
			NativeMethods.BlockInput(true);

			relaxTimer.Tick += new EventHandler(TimerEventProcessor);
			relaxTimer.Interval = 1000;
			relaxTimer.Start();
		}

		private void TimerEventProcessor(object sender, EventArgs e)
		{
			if (--RelaxProgressBar.Value <= 0)
			{
				relaxTimer.Stop();
				this.Close();
				this.Dispose();

				NativeMethods.BlockInput(false);
			}
		}
	}

	internal static class NativeMethods
	{
		[System.Runtime.InteropServices.DllImport("user32.dll")]
		internal static extern bool BlockInput(bool block);
	}
}
